col4 bit P2.0
col3 bit P2.1
col2 bit P2.2
col1 bit P2.3
row1 bit P2.4
row2 bit P2.5
row3 bit P2.6
row4 bit P2.7
keydata equ 70h
keybounc equ 71h
keyport equ P2
org 0h
mov P2,#11111111b
call Init_LCD
start: call keypad4x4 ;calling subroutine keypad4x4
Mov A,keydata ;A = keydata
Cjne A,#0FFh,WrLCD;
sjmp start ;LOOPING FOREVER PART 1
;
WrLCD: Mov R1,#80h ;Pick DDRAM 1st row and 1st col
call write_inst
Mov R1,#30h
Add A,R1
Mov R1,A
call write_data ;write data
Sjmp start ;LOOPING FOREVER PART 2;
;
Init_lcd:
mov r1,#00000001b ;Display clear
call write_inst
mov r1,#00111000b ;Function set,Data 8 bit,2 line font 5x7
call write_inst
mov r1,#00001100b ;Display on,cursor off,cursor blink off
call write_inst
mov r1,#00000110b ;Entry mode,Set increment
call write_inst
ret
;
Write_inst:
clr P3.5 ;P3.5 = RS =0
mov P0,R1 ;copy R1 ke P0
setb P3.7 ;P3.7 =EN = 1
call delay ;call delay time
clr P3.7 ;P3.7 =EN = 0
ret
;
Write_data:
setb P3.5 ;P3.5 = RS =1
mov P0,R1 ;copy R1 ke P0
setb P3.7 ;P3.7 =EN = 1
call delay ;call delay time
clr p3.7 ;P3.7 =EN = 0
ret

;====================================
; subroutine scan keypad 4x4
;====================================
Keypad4x4:
mov keybounc,#50 ;keybounc = 50
mov keyport,#0FFh ;keyport=P2= FF
clr col4 ;col4 = 0
;
keyCOR: jb row4,keyMEN ; Key COR
djnz keybounc,KeyCOR
mov keydata,#0Ah ;Data Output
ret
;
keyMEN: jb row3,keyUpA ; Key MEN
djnz keybounc,keyMEN
mov keydata,#0bh ;Data Output
ret
keyUpA: jb row2,keyDnA ; Key Up Arrow
djnz keybounc,keyUpA
mov keydata,#0ch ;Data Output
ret
;
keyDnA: jb row1,key3 ; Key Down Arrow
djnz keybounc,keyDnA
mov keydata,#0dh ;Data Output
ret
;==========================================
key3: setb col4
clr col3
jb row4,key6
djnz keybounc,key3 ; Key 3
mov keydata,#03h ;Data Output
ret
;
key6: jb row3,key9
djnz keybounc,key6 ; Key 6
mov keydata,#06h ;Data Output
ret
;
key9: jb row2,keyENT
djnz keybounc,key9 ; Key 9
mov keydata,#09h ;Data Output
ret
;
keyENT: jb row1,key2
djnz keybounc,keyENT ; Key ENT
mov keydata,#0eh ;Data Output
ret
;============================================
key2: setb col3

clr col2
jb row4,key5
djnz keybounc,key2
mov keydata,#02h ;Data Output
ret
;
key5: jb row3,key8
djnz keybounc,key5
mov keydata,#05h ; Data Output
ret
;
key8: jb row2,key0
djnz keybounc,key8
mov keydata,#08h ;Data Output
ret
;
key0: jb row1,key1
djnz keybounc,key0
mov keydata,#00h ;Data Output
ret
;==============================================
key1: setb col2
clr col1
jb row4,key4
djnz keybounc,key1
mov keydata,#01h ;Data Output
ret
;
key4: jb row3,key7
djnz keybounc,key4
mov keydata,#04h ;Data Output
ret
;
key7: jb row2,keyCAN
djnz keybounc,key7
mov keydata,#07h ;Data Output
ret
;
keyCAN: jb row1,Nokey
djnz keybounc,keyCAN
mov keydata,#0Fh ;Data Output
ret
;
Nokey: mov keydata,#0FFh
ret
;================================
;The end of Keypad 4x4 subroutine
;================================
delay: mov R0,#001h
delay1: mov R2,#001h

djnz R2,$
djnz R0,delay1
ret
end
